"""

Computational Economics
07: Built-In Functions
http://johnstachurski.net/lectures/builtins.html

"""


"""
    Exercise 1

    Given two numeric lists/tuples X and Y of equal length, compute their inner
    product. Use zip()
"""
X = [1, 2, 3, 4]
Y = [4, 3, 2, 1]
expect = sum([4, 6, 6, 4])

result = sum([x*y for x, y in zip(X, Y)])
assert result == expect


"""
    Exercise 2

    Consider the polynomial

        p(x) = a0 + a1x + a2x^2 + ... anx^n

    Let the list of coefficients be given by coeff = [1, 3, -2, 9, 0, 1]

    Using enumerate(), evaluate the polynomial at x = 7
"""
coeff = [1, 3, -2, 9, 0, 1]
x = 7
result = 0

for index, value in enumerate(coeff):
    if index == 0:
        result += value
    else:
        result += (value * x**index)

print "p(%s) = %s" % (x, result)

book_result = sum(a * x**i for i, a in enumerate(coeff))
assert result == book_result



print '%s: ok' % (__file__)
